/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apuntesdejava.demoswingdbapp.dao.derby;

import com.apuntesdejava.demoswingdbapp.bean.ProductCode;
import com.apuntesdejava.demoswingdbapp.dao.ProductCodeDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author dsilva
 */
public class DerbyProductCodeDao implements ProductCodeDao {

    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public List<ProductCode> findAll() {
        List<ProductCode> list = jdbcTemplate.query("select * from APP.PRODUCT_CODE ", new ProductCodeRowMapper());
        return list;
    }
}

class ProductCodeRowMapper implements RowMapper<ProductCode> {

    @Override
    public ProductCode mapRow(ResultSet rs, int i) throws SQLException {
        ProductCode pc = new ProductCode();
        pc.setProdCode(rs.getString("PROD_CODE"));
        pc.setDescription(rs.getString("DESCRIPTION"));
        return pc;
    }
}
